import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'agencyTaskM',

  state: {
    tableData: { columns: [], dataSource: [], pageInfo: { total: 0, currentPage: 1, numInPage: 20 } },
    poolData: {
      columns: [],
      dataSource: {
        flowEditVo: { item: [] },
      },
    },
    hisData: { dataSource: [], columns: [] }, // 历史审批信息
    selectData: {},
    selectData2: {},
    tagData: [],
    poolNameData: [],
    currentUser: {},
    detailData: {},
  },

  effects: {
    // 获取标签
    *findTag({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.findTag, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            tagData: res,
          },
        });
      }
    },
    // 获取券池名称
    *getPoolName({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getPoolName, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            poolNameData: res,
          },
        });
      } else {
        message.error('获取券池名称失败');
      }
    },

    // 获取券池审批信息
    *getPoolData({ payload, callback }, { call, put }) {
      const id = payload.id;
      const query = { id: id };
      const res = yield call(api.agencyTask.approvalList.pool, query);

      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            poolData: res.data,
          },
        });
        callback(res.data);
      } else {
        callback();
        message.error(res.msg);
      }
    },
    // 查询详细信息
    *getDetails({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getDetails, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            detailData: res.data.dataSource,
          },
        });
        callback(res.data);
      } else {
        message.error('查询列表失败');
      }
    },

    // 通过或拒绝
    *agreeOrNo({ payload, callback }, { call, put }) {
      const res = yield call(api.agencyTask.agreeOrNo, payload);
      if (res && res.code == 0) {
        message.success('操作成功');
        callback(true);
      } else {
        message.error('操作失败,失败原因：' + res.msg);
        callback(false);
      }
    },
    // 获取券池审批历史
    *getHistory({ payload, callback }, { call, put }) {
      const res = yield call(api.agencyTask.getHistory, payload);

      if (res && res.code == 0) {
        // yield put({
        //   type: 'save',
        //   payload: {
        //       hisData: res.data,
        //   },
        // });
        callback(res.data);
      } else {
        callback();
        message.error('查询审批历史失败！');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
